import re

from apps.ext import db
# 多继承
from flask_login import UserMixin
# 加密
from werkzeug.security import generate_password_hash,check_password_hash


class User(db.Model,UserMixin):
    uid = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(64),unique=True,nullable=False,index=True)
    _password_hash = db.Column(db.String(128), unique=True, nullable=False)
    email = db.Column(db.String(64), unique=True, nullable=False,index=True)
    phone = db.Column(db.String(64), unique=True, index=True, nullable=False)
    is_active = db.Column(db.Boolean,default=False)
    # 当主键不为id时必须重写该方法
    def get_id(self):
        return self.uid

    # 获取私有属性 password = user.password
    @property
    def password(self):
        return self._password_hash

    # 设置私有属性 user.password = "1111111"
    @password.setter
    def password(self,password):
        # if re.findall('^[A-Z]{1}(\w){5,11}',password):
        if password:
            self._password_hash = generate_password_hash(password=password)
        else:
            raise Exception('密码不符合规范')

    """
    删除私有属性 del user.password
    @password.deleter
    def password(self):
        del self._password_hash
    """

    def vertify_password(self,password):
        return check_password_hash(self._password_hash,password=password)




"""
装饰器:
def w1(func):
    def inner():
        # 验证1
        # 验证2
        # 验证3
        func()
    return inner
"""